
from optparse import OptionParser
import os
import sys

sys.path.append('./Lib')
import util
import HTMLTestRunner

if __name__  ==  '__main__':
    
    parser = OptionParser()
    
    defaultServer = 'http://10.32.191.225:5988'
    defaultNamespace = 'root/emc'
    defaultUsername = 'admin'
    defaultPassword = '#1Password'
    defaultLogFile = './Report/smis.html'
    
    ## Connection Parameters    
    parser.add_option('-s', '--cimserver', help = 'cim server https connection [default: %default]',
                      default = defaultServer, dest = 'cimserver')
    parser.add_option('-u', '--username', help = 'cim user [default: %default]',
                      default = defaultUsername, dest = 'cimusr')
    parser.add_option('-p', '--password', help = 'cim password [default: %default]',
                      default = defaultPassword, dest = 'cimpwd')
    parser.add_option('-n', '--namespace', help = 'namespace used [default: %default]',
                      default = defaultNamespace, dest = 'cimns')
    
    ## Operation Parameters
    parser.add_option('-c', '--cases', dest = 'cases',
                      help = 'test cases to be run.')
    parser.add_option('-t', '--threadsNum', dest = 'threadsNum',
                      help = 'how many threads to run the cases')
    parser.add_option('-l', '--logfile', dest = 'logfile', default = defaultLogFile,
                      help = 'html file to store the log information')
    parser.add_option('-f', '--casefile', dest = 'casefile',
                      help = """the file listing the test case information. """
                      """Each line in this file: module, suite, case. Support * for all.""")
    
    ## Parse user input
    (options, args) = parser.parse_args()
    if (not options.cases) and (not options.casefile):
        parser.print_help()
        exit(1)
        
    ## Store the connection parameters
    connectionParas = [('cimserver', options.cimserver), ('cimusr', options.cimusr),
        ('cimpwd', options.cimpwd), ('cimns', options.cimns)]
    for name, value in connectionParas:
        os.environ.__setitem__(name, value)
    
    ## Parse test cases
    casesStr = '' if not options.cases else options.cases
    allSuites = util.loadTestCases(casesStr, options.casefile)
    
    with open(options.logfile, 'wb') as logStream:
        runner = HTMLTestRunner.HTMLTestRunner(stream = logStream, verbosity = 2)
        runner.run(allSuites)
        #runner.runInThreads(suite, 3)
    exit(0)